package com.whnk.attendance.controller;

import com.whnk.attendance.service.IPmProInfoService;
import com.whnk.attendance.vo.*;
import com.whnk.common.core.utils.poi.ExcelUtil;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.common.core.web.page.TableDataInfo;
import com.whnk.common.log.annotation.Log;
import com.whnk.common.log.enums.BusinessType;
import com.whnk.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/pmProInfo")
@Api(tags="项目列表")
public class PmProInfoController extends BaseController {

    @Autowired
    private IPmProInfoService pmProInfoService;

    /*
     * 分页查询项目列表
     */
    @PreAuthorize(hasPermi = "attendance:info:list")
    @GetMapping("/list")
    @ApiOperation(value = "分页查询项目列表", notes = "分页查询项目列表")
    public TableDataInfo list(@ApiParam(value = "项目信息", required = false) PmProInfoQueryVo pmProInfoQueryVo)
    {
        startPage();
        List<PmProInfoListVo> list = pmProInfoService.selectPmProInfoListVoList(pmProInfoQueryVo);
        return getDataTable(list);
    }

    /**
     * 根据逻辑主键查询项目信息
     */
    @PreAuthorize(hasPermi = "attendance:info:query")
    @GetMapping(value = "/query/{proId}")
    @ApiOperation(value = "根据项目主键查询项目信息", notes = "根据项目主键查询项目信息")
    public AjaxResult getInfo(@ApiParam(value = "项目主键", required = false) @PathVariable("proId") Long proId)
    {
        return AjaxResult.success(pmProInfoService.selectPmProInfoByProId(proId));
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize(hasPermi = "attendance:info:add")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增项目信息", notes = "新增项目信息")
    public AjaxResult add(@ApiParam(value = "项目信息", required = false) @Validated @RequestBody PmProInfoUpdateVo pmProInfoUpdateVo)
    {
        if(!pmProInfoService.checkProNum(pmProInfoUpdateVo)){
            return AjaxResult.error("新增项目信息" + pmProInfoUpdateVo.getProNum() + "失败，该项目信息已存在");
        }
        return toAjax(pmProInfoService.insertPmProInfo(pmProInfoUpdateVo));
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize(hasPermi = "attendance:info:edit")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation(value = "修改项目信息", notes = "修改项目信息")
    public AjaxResult edit(@ApiParam(value = "项目信息", required = false) @Validated @RequestBody PmProInfoUpdateVo pmProInfoUpdateVo)
    {
        if(!pmProInfoService.checkProNum(pmProInfoUpdateVo)){
            return AjaxResult.error("修改项目信息" + pmProInfoUpdateVo.getProNum() + "失败，该项目信息已存在");
        }
        return toAjax(pmProInfoService.updatePmProInfo(pmProInfoUpdateVo));
    }

    /**
     * 批量删除项目信息
     */
    @PreAuthorize(hasPermi = "attendance:info:remove")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove/{proIds}")
    @ApiOperation(value = "批量删除项目信息", notes = "批量删除项目信息")
    public AjaxResult remove(@ApiParam(value = "项目主键", required = false) @PathVariable Long[] proIds)
    {
        return toAjax(pmProInfoService.deletePmProInfoByProIds(proIds));
    }

    /**
     * 导出项目信息
     */
    @PreAuthorize(hasPermi = "attendance:info:export")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出项目信息", notes = "导出项目信息")
    public void export(HttpServletResponse response, @ApiParam(value = "项目信息", required = false) PmProInfoQueryVo pmProInfoQueryVo) throws IOException
    {
        List<PmProInfoListVo> list = pmProInfoService.selectPmProInfoListVoList(pmProInfoQueryVo);
        ExcelUtil<PmProInfoListVo> util = new ExcelUtil<>(PmProInfoListVo.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 导入项目信息
     */
    @PreAuthorize(hasPermi = "attendance:info:import")
    @Log(title = "项目信息", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    @ApiOperation(value = "导入项目信息", notes = "导入项目信息")
    public AjaxResult importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PmProInfoListVo> util = new ExcelUtil<>(PmProInfoListVo.class);
        List<PmProInfoListVo> pmProInfoList = util.importExcel(file.getInputStream());
        String message = pmProInfoService.importData(pmProInfoList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 项目编号id，项目名称name接口
     */
    @GetMapping("/proInfoList")
    @ApiOperation(value = "项目编号id，项目名称name接口", notes = "项目编号id，项目名称name接口")
    public TableDataInfo proInfoList(){
        List<PmProInfoListVo> list = pmProInfoService.proInfoList();
        return getDataTable(list);
    }
//    /**
//     * 查询界面项目分类名称接口
//     */
//    @PostMapping("/selectList")
//    @ApiOperation(value = "查询界面项目分类名称接口", notes = "查询界面项目分类名称接口")
//    public TableDataInfo proClaNameList(){
//        List<PmProInfoProClaNameVo> list = new ArrayList<>();
//        PmProInfoProClaNameVo pmProInfoProClaNameVo = new PmProInfoProClaNameVo();
//        list.add(pmProInfoProClaNameVo);
//        return getDataTable(list);
//    }

//    /**
//     * 新增修改界面项目分类下拉框
//     */
//    @PostMapping("/addList")
//    @ApiOperation(value = "新增修改界面项目分类下拉框", notes = "新增修改界面项目分类下拉框")
//    public TableDataInfo proClaNumList(){
//        List<PmProInfoProClaNumVo> list = pmProInfoService.selectPmProInfoProClaNumVoList();
//        return getDataTable(list);
//    }

//    /**
//     * 查询界面隶属项目下拉框接口
//     */
//    @PostMapping("/selectProInfoList")
//    @ApiOperation(value = "隶属项目下拉框接口",notes = "隶属项目下拉框接口")
//    public AjaxResult selectProInfoList(){
//        List<MonthEmpStatisProNumVo> list = pmProInfoService.selectPmProInfoQueryList();
//        return AjaxResult.success(list);
//    }
}
